import Vue from 'vue';
import Router from 'vue-router';
import store from '~/state';

const Index = resolve => require( ['~/views/Index.vue'], resolve);
const GoodCategories = resolve => require( ['~/views/GoodCategories.vue'], resolve);
const Me = resolve => require( ['~/views/Me.vue'], resolve);
const Auth = resolve => require( ['~/views/Auth.vue'], resolve);
const Login = resolve => require( ['~/views/Auth/Login.vue'], resolve);
const Register = resolve => require( ['~/views/Auth/Register.vue'], resolve);
const Forgot = resolve => require( ['~/views/Auth/Forgot.vue'], resolve);
const Goods = resolve => require( ['~/views/Goods.vue'], resolve);
const GoodsDetail = resolve => require( ['~/views/Goods/GoodsDetail.vue'], resolve);
const Help = resolve => require( ['~/views/Help.vue'], resolve);
const HelpTypes = resolve => require( ['~/views/Help/HelpTypes.vue'], resolve);
const HelpDetail = resolve => require( ['~/views/Help/HelpDetail.vue'], resolve);
const Cart = resolve => require( ['~/views/Cart.vue'], resolve);
const Favorites = resolve => require( ['~/views/Me/Favorites.vue'], resolve);
const Evaluates = resolve => require( ['~/views/Me/Evaluates.vue'], resolve);
const FootPrint = resolve => require( ['~/views/Me/FootPrint.vue'], resolve);
const Orders = resolve => require( ['~/views/Me/Orders.vue'], resolve);
const OrderDetail = resolve => require( ['~/views/Me/OrderDetail.vue'], resolve);
const Balance = resolve => require( ['~/views/Me/Balance.vue'], resolve);
const Level = resolve => require( ['~/views/Me/Level.vue'], resolve);
const SlAddress = resolve => require( ['~/views/Me/SlAddress.vue'], resolve);
const Profile = resolve => require( ['~/views/Me/Profile.vue'], resolve);
const Pay = resolve => require( ['~/views/Order/Pay.vue'], resolve);
const MoneyLog = resolve => require( ['~/views/Me/MoneyLog.vue'], resolve);


Vue.use(Router);

var router = new Router({
  routes: [
    {
        path: '/',
        name: 'Index',
        component: Index
    },
    {
        path: '/good-categories',
        name: 'GoodCategories',
        component: GoodCategories
    },
    {
        path: '/me',
        name: 'Me',
        meta: {
            requireAuth: true
        },
        component: Me
    },
    {
        path: '/me/favorites',
        component: Favorites
    },
    {
        path: '/me/evaluates',
        component: Evaluates,
    },
    {
        path: '/me/footprints',
        component: FootPrint,
    },
    {
        path: '/me/orders',
        component: Orders,
    },
    {
        path: '/me/order/detail/:id',
        component: OrderDetail
    },
    {
        path: '/me/balance',
        component: Balance,
    },
    {
        path: '/me/level',
        component: Level,
    },
    {
        path: '/me/address',
        component: SlAddress
    },
    {
        path: '/me/profile',
        component: Profile
    },
    {
        path: '/me/money-logs',
        component: MoneyLog
    },
    {
        path: '/order/pay/:id',
        component: Pay
    },
    {
        path: '/auth',
        component: Auth,
        children: [
            {
                path: 'login',
                component: Login
            },
            {
                path: 'register',
                component: Register
            },
            {
                path: 'forgot',
                component: Forgot
            }
        ]
    },
    {
        path:'/goods/search',
        component: Goods
    },
    {
        path: '/goods/detail/:id',
        component: GoodsDetail
    },
    {
        path: '/cart',
        component: Cart
    },
    {
        path: '/help',
        component: Help,
        children:[
            {
                path:'',
                component: HelpTypes
            },
            {
                path: ':type/:name',
                component: HelpDetail
            }
        ]
    }
  ],
  linkActiveClass: 'active'
});

router.beforeEach((to, from, next) => {
    if (!to.meta.requireAuth) {
        next();
        return;
    }
    if (store.state.guest == true) {
        next({
            path: '/auth/login',
            query: {
                redirect: to.fullPath
            }
        });
        return;
    }

    next();
});

export default router;
